using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Filter;
using log4net.Layout;
using log4net.Repository.Hierarchy;

namespace Triton.Common.LogUtilities
{
	public class CustomLogger
	{
		private Level level_0 = Level.All;

		private Level level_1 = Level.Emergency;

		private readonly string string_0;

		private WpfRtfAppender wpfRtfAppender_0;

		[CompilerGenerated]
		private string string_1;

		public string FileName
		{
			[CompilerGenerated]
			get
			{
				return string_1;
			}
			[CompilerGenerated]
			private set
			{
				string_1 = value;
			}
		}

		public CustomLogger(string path, string prefix, Level minLevel, Level maxLevel)
		{
			string_0 = prefix;
			level_0 = minLevel ?? Level.All;
			level_1 = maxLevel ?? Level.Emergency;
			Directory.CreateDirectory(path);
			PatternLayout patternLayout = new PatternLayout("%-4utcdate [%thread] %-5level %logger{1} %ndc - %message%newline");
			DateTime now = DateTime.Now;
			FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), string.Format("{6}\\{7} {5} {0:0000}-{1:00}-{2:00} {3:00}.{4:00}.txt", now.Year, now.Month, now.Day, now.Hour, now.Minute, Process.GetCurrentProcess().Id, path, string_0));
			FileAppender fileAppender = new FileAppender
			{
				File = FileName,
				Layout = patternLayout,
				LockingModel = new FileAppender.MinimalLock()
			};
			fileAppender.AddFilter(new LevelRangeFilter
			{
				LevelMin = level_0,
				LevelMax = level_1,
				AcceptOnMatch = true
			});
			patternLayout.ActivateOptions();
			fileAppender.ActivateOptions();
			AsyncAppender asyncAppender = new AsyncAppender();
			asyncAppender.AddAppender(fileAppender);
			asyncAppender.ActivateOptions();
			Hierarchy obj = (Hierarchy)LogManager.CreateRepository(string_0);
			obj.Root.AddAppender(asyncAppender);
			obj.Configured = true;
		}

		public void ChangeLogFilterLevel(Level minLevel = null, Level maxLevel = null)
		{
			if (minLevel != null)
			{
				level_0 = minLevel;
			}
			if (maxLevel != null)
			{
				level_1 = maxLevel;
			}
			if (wpfRtfAppender_0 != null)
			{
				wpfRtfAppender_0.ClearFilters();
				wpfRtfAppender_0.AddFilter(new LevelRangeFilter
				{
					LevelMin = level_0,
					LevelMax = level_1,
					AcceptOnMatch = true
				});
				wpfRtfAppender_0.AddFilter(new DenyAllFilter());
			}
		}

		public void Clear()
		{
			if (wpfRtfAppender_0 != null)
			{
				wpfRtfAppender_0.Clear();
			}
		}

		public void AddWpfListener(ScrollViewer scrollViewer, RichTextBox rtbLog)
		{
			PatternLayout patternLayout = new PatternLayout("%message");
			wpfRtfAppender_0 = new WpfRtfAppender(scrollViewer, rtbLog)
			{
				Layout = patternLayout
			};
			LevelRangeFilter levelRangeFilter = new LevelRangeFilter();
			levelRangeFilter.LevelMin = level_0;
			levelRangeFilter.LevelMax = level_1;
			wpfRtfAppender_0.AddFilter(levelRangeFilter);
			patternLayout.ActivateOptions();
			wpfRtfAppender_0.ActivateOptions();
			AsyncAppender asyncAppender = new AsyncAppender();
			asyncAppender.AddAppender(wpfRtfAppender_0);
			asyncAppender.ActivateOptions();
			((Hierarchy)LogManager.GetRepository(string_0)).Root.AddAppender(asyncAppender);
		}

		public ILog GetLoggerInstanceForType()
		{
			return LogManager.GetLogger(string_0, new StackTrace().GetFrames()[1].GetMethod().DeclaringType);
		}
	}
}
